/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package com.isisufes.joinus.appserver;

import java.lang.Integer; //
import java.sql.*;


public class Contact extends Servlet1
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 5813366953801328991L;
	private PreparedStatement ps_isf, ps_add, ps_del, ps_get, ps_search;

    protected void initStmt()
    {
        ps_isf = DBConn.stmt("select * from Contact where idProfile=? and idProfileDst=?");
        ps_add = DBConn.stmt("insert into Contact(idProfile,idProfileDst) values(?,?)");
        ps_del = DBConn.stmt("delete from Contact where idProfile=? and idProfileDst=?");
        ps_get = DBConn.stmt("select idProfileDst as idProfile from Contact where idProfile=?");
        ps_search = DBConn.stmt("select idProfile from Profile where firstName || ' ' || lastName ~* ?");
    }

    private boolean isFriend(String dst) throws Exception
    {
        ps_isf.clearParameters();
        ps_isf.setInt(1, Integer.parseInt(id));  // ps_isf.setString(1, id);
        ps_isf.setInt(2, Integer.parseInt(dst));  //ps_isf.setString(2, dst);

        ResultSet rs = ps_isf.executeQuery();
        boolean ret = rs.next();

        rs.close();
        return ret;
    }

    private String add(String dst) throws Exception
    {
        if (!isFriend(dst)) {
            ps_add.clearParameters();
            ps_add.setInt(1, Integer.parseInt(id));  // ps_add.setString(1, id);
            ps_add.setInt(2, Integer.parseInt(dst));  // ps_add.setString(2, dst);
            ps_add.executeUpdate();
        }

        return "";
    }

    private String remove(String dst) throws Exception
    {
        if (isFriend(dst)) {
            ps_del.clearParameters();
            ps_del.setInt(1, Integer.parseInt(id));  //            ps_del.setString(1, id);
            ps_del.setInt(2, Integer.parseInt(dst));  //            ps_del.setString(2, dst);
            ps_del.executeUpdate();
        }

        return "";
    }

    private String strContact(ResultSet rs) throws Exception
    {
        StringBuilder sb = new StringBuilder();

        sb.append("<Contacts>\n");

        while (rs.next())
            sb.append("<Contact>"+rs.getString("idProfile")+"</Contact>");

        sb.append("</Contacts>\n");

        rs.close();
        return sb.toString();
    }

    private String get() throws Exception
    {
        ps_get.clearParameters();
        ps_get.setInt(1, Integer.parseInt(id));  //        ps_get.setString(1, id);

        return strContact(ps_get.executeQuery());
    }

    private String search(String q) throws Exception
    {
        ps_search.clearParameters();
        ps_search.setString(1, q);

        return strContact(ps_search.executeQuery());
    }

    protected String dispatch(String action) throws Exception
    {
        String ret = null;

        if (action.equals("addUser")) ret = add(arg("id"));
        if (action.equals("removeUser")) ret = remove(arg("id"));
        if (action.equals("searchUser")) ret = search(arg("q"));
        if (action.equals("getUsers")) ret = get();

        return ret;
    }
}
